package com.shop.cereshop.agent.service.referral.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.shop.cereshop.agent.page.referral.AgentGetIncomeVO;
import com.shop.cereshop.agent.page.referral.AgentShopDetailVO;
import com.shop.cereshop.agent.page.referral.AgentShopOrderDetailVO;
import com.shop.cereshop.agent.page.referral.AgentShopVO;
import com.shop.cereshop.agent.page.referral.AgentShopsVO;
import com.shop.cereshop.commons.constant.IntegerEnum;
import com.shop.cereshop.commons.domain.referral.AgentReferralRelation;
import com.shop.cereshop.agent.dao.referral.AgentReferralDAO;
import com.shop.cereshop.agent.service.referral.AgentReferralService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class AgentReferralServiceImpl extends ServiceImpl<AgentReferralDAO, AgentReferralRelation>
    implements AgentReferralService {

  private final AgentReferralDAO agentReferralDAO;

  @Override
  public AgentGetIncomeVO getIncome(Long agentUserId) {
    // 查询当前代理商所代理城市的所有商户分润以及门店分润
    AgentGetIncomeVO agentGetIncomeVO = agentReferralDAO.getIncome(agentUserId);
    return agentGetIncomeVO;
  }

  @Override
  public AgentShopsVO getShops(Long agentUserId, Integer page, Integer pageSize, Integer shopType,
      String search, Integer sort, Integer timeRange) {
    // 设置默认值
    if (timeRange == null) {
      //默认本月
      timeRange = IntegerEnum.TIME_RANGE_MONTH.getCode();
    }

    // 1. 查询总数和总分润（全局统计，不受类型和搜索影响）
    AgentShopsVO statistics = agentReferralDAO.getShopStatistics(agentUserId, timeRange);

    // 2. 查询店铺列表（分页，支持类型和搜索筛选）
    PageHelper.startPage(page, pageSize);
    List<AgentShopVO> shopList = agentReferralDAO.getShops(agentUserId, shopType, search, sort, timeRange);

    // 3. 组装返回结果
    statistics.setShopList(shopList);
    return statistics;
  }

  @Override
  public AgentShopDetailVO getShopDetail(Long agentUserId, Long shopId, Integer page, Integer pageSize, String month) {
    // 1. 查询店铺基本信息和统计数据
    AgentShopDetailVO shopDetail = agentReferralDAO.getShopBasicInfo(agentUserId, shopId);
    if (shopDetail == null) {
      return null;
    }

    // 2. 查询订单列表（分页）
    PageHelper.startPage(page, pageSize);
    List<AgentShopDetailVO.ShopOrderVO> orderList = agentReferralDAO.getShopOrders(shopId, month);
    shopDetail.setOrderList(orderList);

    return shopDetail;
  }

  @Override
  public AgentShopOrderDetailVO getShopOrderDetail(Long agentUserId, Long orderId) {
    // 查询订单详情和分润信息
    AgentShopOrderDetailVO orderDetail = agentReferralDAO.getShopOrderDetail(agentUserId, orderId);
    if (orderDetail == null) {
      return null;
    }

    // 查询订单商品列表
    List<AgentShopOrderDetailVO.OrderGoodsVO> goodsList = agentReferralDAO.getOrderGoods(orderId);
    orderDetail.setGoodsList(goodsList);

    return orderDetail;
  }
}